<?php
namespace Com\Pay;
class TongyiPay{

    private $config = array();

  
    public function __construct($send_data){
        $this->config = $send_data;
    }


    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
        $returnUrl    = $order_info['return_url'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/TongyiPayNotify';

        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'title' => "Cbpay",
            "body" => 'Cbpay',
            'charset' =>   'utf-8',
            "merchantId"  => $this->config['mch_id'],
            "notifyUrl"  =>  $notifyUrl,
            "orderNo"  =>  $order_sn,
            'paymentType' => '1',
            'paymethod' => "directPay",
            'returnUrl' => $returnUrl,
            'service' => "online_pay",
            'totalFee'    => $price,
            "isApp" => ($order_info['payType'] == 'QQPAY') ? 'app' : 'web',
            'defaultbank' => $order_info['payType'],
            'signType' => 'SHA',
//            'userIp' => get_client_ip(),
//            'appName' => 'Cbpay',
//            'appMsg' => 'Cbpay',
//            'appType' => 'wap',
//            'backUrl' => $returnUrl
        );
        $ContentSign = $this->signature($post_data,$this->config['mch_key']); // 计算出Sign
        $post_data['sign'] = $ContentSign; // Sign签名
        $tar_url = "https://ebank.ztpo.cn/payment/v1/order/{$this->config['mch_id']}-{$order_sn}";
        $res = $this->curl_post($tar_url,$post_data);
        if($order_info['payType'] == 'QQPAY') {
            $res = json_decode($res , true);
            return $res['codeUrl'];
        }else{
            return $res;
        }
    }

    private function signature($params, $key){
        unset($params['signType']);
        unset($params['sign']);
        ksort($params);
        $string = "";
        foreach ($params as $name => $value) {
            $string .= $name . '=' . $value . '&';
        }
        $string = substr($string, 0, strlen($string) -1 );
        $string .= $key;
        return strtoupper(sha1($string));
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $MySign = $this->signature($data,$this->config['mch_key']);
        if($data['sign'] == $MySign){
            if($data['is_success'] == 'T'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }

    public function curl_post($url, $data)
    {
        $data = http_build_query($data);
        // file_put_contents('./tmp.txt',$data);
        //echo '<br>'.$data.'<br>';
        // mb_strlen($str,'utf-8');
        // var_dump((mb_strlen($data,'utf-8') == strlen($data)));
        $opts = array(
            'http'=>array(
                'method'=>"POST",
                'timeout'=>60,
                'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-length:".mb_strlen($data,'utf-8')."\r\n" .
                    "Cookie: foo=bar\r\n" .
                    "\r\n",
                'content' => $data,
            )
        );
        $cxContext = stream_context_create($opts);
        $result = file_get_contents($url, false, $cxContext);
        return $result;
    }
}
